package com.nave.segundaguerra.servidorecliente.cliente;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.Log;
import android.widget.RelativeLayout;

import com.nave.segundaguerra.servidorecliente.util.ActivityUtil;
import com.nave.segundaguerra.servidorecliente.util.ImageLibrary;
import com.nave.segundaguerra.servidorecliente.util.Protocolo;

public class SoldadoCliente extends PlayerCliente{

	private Bitmap soldado;
	private Bitmap tiro;
	private float angle;
	private float angleFire;
	private Point positionTiro = new Point();
	public SoldadoCliente(String nome, PointF position) {
		super(nome, position, "Soldado");
		
	}
	@Override
	public void draw(Canvas canvas)
	{	
		if(this.soldado == null){
			this.soldado = this.getImage();
			this.tiro = this.getTiro();
		}
		
		if(this.getRotateImage()){
			this.angle = (float)this.getAngle(this.getPosition().x, this.getPosition().y, this.getDestination().x, this.getDestination().y);
			this.soldado = rotate(this.getImage(), angle);
			this.setRotateImage(false);
		}
		if(this.getRotateImageTiro()){
			this.angleFire = (float)this.getAngleFire(this.getPosition().x, this.getPosition().y, this.getDestinationTiro().x, this.getDestinationTiro().y);
			this.tiro = rotate(this.getTiro(), angleFire);
			this.setRotateImageTiro(false);
		}
		
		if(this.soldado != null){
			canvas.drawBitmap(this.soldado,this.getPosition().x, this.getPosition().y, this.getPaint());
			
		}else{
			canvas.drawCircle(this.getPosition().x, this.getPosition().y, 10, this.getPaint());
		}
		if(this.isAtirou()){
			this.angle = (float)this.getAngle(this.getPosition().x, this.getPosition().y, this.getDestinationTiro().x, this.getDestinationTiro().y);
			this.soldado = rotate(this.getImage(), angle);
			
			this.positionTiro.x = (int)this.getPosition().x;
			this.positionTiro.y = (int)this.getPosition().y;
			
			if((this.getDestinationTiro().x - this.getPosition().x) < 0){
				this.positionTiro.x -= this.getTiro().getWidth();
			}
			if((this.getDestinationTiro().y - this.getPosition().y) < 0){
				this.positionTiro.y -= this.getTiro().getWidth();
			}
			canvas.drawBitmap(this.tiro,this.positionTiro.x,this.positionTiro.y,this.getPaint());
			this.Atirou(false);
		}
	}
	public Bitmap rotate(Bitmap b, float degrees) {
		Bitmap b2 = b;
	    if (degrees != 0 && b != null) {
	        Matrix m = new Matrix();
	        m.postRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
	        try {
	            b2 = Bitmap.createBitmap(
	                    b, 0, 0, b.getWidth(), b.getHeight(), m, true);
	        } catch (OutOfMemoryError ex) {
	           throw ex;
	        }
	    }
	    return b2;
	}
	@Override
	public void sendTiro(Point toque){
		this.getDadosCliente().getCliente().Send(Protocolo.PROTOCOL_SHOOT + "," + this.getNome() + "," + toque.x + "," + toque.y);
	}
	
	@Override
	public void sendNewTiro(Point toque){

		this.getDadosCliente().getCliente().Send(Protocolo.PROTOCOL_SHOOT + "," + this.getNome()+ "," + toque.x + "," + toque.y);
	}
	
	@Override
	public void sendPos(PointF pos){
		this.getDadosCliente().setX(pos.x);
		this.getDadosCliente().setY(pos.y);
		this.getDadosCliente().sendMove();
	}
	
	
	@Override
	public void dirViewPort(Point pnt1, Point pnt2, Point tamCanvas, RelativeLayout container, float scale){
		
		if (!((container.getScrollX() + this.getPosition().x / 2) < 0)) {
			if (container.getScrollX() + ActivityUtil.getDisplayWidth() < tamCanvas.x) {
				container.scrollTo((int)(this.getPosition().x / 2), container.getScrollY());
//				if(container.getScrollX() > tamCanvas.x){
//					container.scrollTo(0, (container.getScrollX() -1));
//				}
				Log.i("PosicaoX", "" + this.getPosition().x);
			}
		}
		if (!((container.getScrollY() + this.getPosition().y / 2) < 0)) {
			if (container.getScrollY() + ActivityUtil.getDisplayHeight() < tamCanvas.y) {
				container.scrollTo(container.getScrollX(), (int)(this.getPosition().y / 2));
//				if(container.getScrollY() > tamCanvas.y){
//					container.scrollTo(0, (container.getScrollY() -1));
//				}
				Log.i("PosicaoY", "" + this.getPosition().y);
			}
		}
	}
}
